import 'package:flutter/material.dart';
import '../../helper/Helper.dart';
import '../../model/daydata.dart';
import '../../model/sum_day.dart';
import '../../model/swxg.dart';
import '../../model/xhnl.dart';

import '../../helper/channelHelper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';

class DayDataListPage extends StatefulWidget {
  @override
  DayDataListPage();

  @override
  DayDataListPageState createState() => new DayDataListPageState();
}

class DayDataListPageState extends State<DayDataListPage> {
  final TextEditingController c1 = new TextEditingController();
  final TextEditingController c2 = new TextEditingController();

  // ignore: null_check_always_fails
  late SumDay x1;
  bool ifOK = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    c2.text = Helper.DateTimeYestodayString();
    c1.text = Helper.DateTimeMonthString();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(title: Text("水情日表统计")),
      // floatingActionButton: getFloatingButton(context),
      body: DayDataListBody(context),
    );
  }

  void setQ() async {
    var x2 = await SumDay.Get(c1.text, c2.text);
    try {
      ifOK = false;
      x1 = await SumDay.Get(c1.text, c2.text) as SumDay;
      print(x1.toJson());
      ifOK = true;
      this.setState(() => {});
      ChannelHelper.showToast("计算完成！");
    } catch (e) {
      print(e.toString());
      ChannelHelper.showToast("计算错误！" + e.toString());
    }
  }

  String getStr(double xx) {
    return Helper.RoundDouble(xx, 1, " 万m3");
  }

  String getStr2(double xx) {
    return Helper.RoundDouble(xx, 2, " m");
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];

    list2.add(InputWidget.getInput("开始日期", c1));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getInput("结束日期", c2));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getButton("计算", setQ));

    list1.add(InputWidget.inCard2(list2));
    if (!ifOK) return list1;

    var showColor1 = Colors.blue;
    var vL = <Widget>[];
    vL.add(getShowItem("平均入库流量", x1.AverR, 0));
    vL.add(getShowItem("最大入库流量", x1.MaxR, 0));
    vL.add(getShowItem("最小入库流量", x1.MinR, 0));

    vL.add(getShowItem("平均出库流量", x1.AverQ, 0));
    vL.add(getShowItem("平均发电流量", x1.AverQf, 0));
    vL.add(getShowItem("平均弃水流量", x1.AverQp, 0));

    vL.add(getShowItem("平均库水位", x1.AverH1, 2));
    vL.add(getShowItem("最高库水位", x1.MaxH1, 2));
    vL.add(getShowItem("最低库水位", x1.MinH1, 2));

    vL.add(getShowItem("平均耗水率", x1.AverHSL, 2));
    vL.add(getShowItem("平均发电负荷率", x1.AverFDFHL, 2));
    vL.add(getShowItem("总上网电量", x1.SumSWDL, 0));

    vL.add(getShowItem("总降雨量", x1.SumP, 1));
    vL.add(getShowItem("总来水量", x1.SumWr, 0));
    vL.add(getShowItem("总泄流量", x1.SumWz, 0));
    vL.add(getShowItem("总弃水量", x1.SumWq, 0));
    vL.add(getShowItem("总弃水损失电量", x1.SumTFQS, 0));
    vL.add(getShowItem("总发电量", x1.SumZFDL, 0));

    list3.add(InputWidget.getShowRow([vL[0], vL[1], vL[2]], true));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getShowRow([vL[3], vL[4], vL[5]], true));
    list3.add(InputWidget.getDivider());
    list3.add(InputWidget.getShowRow([vL[6], vL[7], vL[8]], true));

    list4.add(InputWidget.getShowRow([vL[12], vL[13]], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([vL[14], vL[11]], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([vL[15], vL[16]], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([vL[9], vL[10]], true));

    if (ifOK) {
      list1.add(InputWidget.inCard2(list3));
      list1.add(InputWidget.inCard2(list4));
      list1.add(InputWidget.inCard(dayListGrid(x1.DList)));
    }

    return list1;
  }

  String getCellValue(DayData a, int j) {
    return Helper.RoundDouble(a.AverQz, 0, "");
  }

  Widget dayListGrid(List<DayData> data) {
    var x1 = <DataRow>[];
    var x2 = <DataRow>[];

    for (var a in data) {
      x1.add(DataRow(cells: [
        DataCell(Text(Helper.DateTimeFormat2(a.TM))),
      ]));

      x2.add(DataRow(cells: [
        DataCell(Text(Helper.RoundDouble(a.AverH1, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.AverP, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.AverR, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.MaxR, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.MinR, 0, ""))),

        DataCell(Text(Helper.RoundDouble(a.AverQz, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.AverQf, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.AverQp, 0, ""))),

        //DataCell(Text(Helper.RoundDouble( a.AverA,0,""))),
        DataCell(Text(Helper.RoundDouble(a.MaxFdP, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.MinFdP, 1, ""))),
        DataCell(Text(Helper.RoundDouble(a.AverFdP, 1, ""))),

        DataCell(Text(Helper.RoundDouble(a.FdFHL, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.FdHSL, 2, ""))),
        DataCell(Text(Helper.RoundDouble(a.FdJH, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.SWDL, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.ZFdL, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FdHour1 * 1.0, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FdHour2 * 1.0, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FdHour3 * 1.0, 0, ""))),
        DataCell(Text(Helper.RoundDouble(a.FdHour4 * 1.0, 0, ""))),

        DataCell(Text(Helper.RoundDouble(a.H0, 2, ""))),
      ]));
    }

    return Container(
        height: 600.0,
        child: SingleChildScrollView(
            child: Row(children: [
          Container(
              width: 120.0,
              child: DataTable(columns: [
                DataColumn(label: Text('日期')),
              ], rows: x1)),
          Expanded(
              child: SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: DataTable(columns: [
                    DataColumn(numeric: true, label: Text('平均库水位')),
                    DataColumn(numeric: true, label: Text('流域降雨量')),
                    DataColumn(numeric: true, label: Text('入库流量')),
                    DataColumn(numeric: true, label: Text('最大入库')),
                    DataColumn(numeric: true, label: Text('最小入库')),
                    DataColumn(numeric: true, label: Text('出库流量')),
                    DataColumn(numeric: true, label: Text('发电流量')),
                    DataColumn(numeric: true, label: Text('弃水流量')),
                    DataColumn(numeric: true, label: Text('最大发电负荷')),
                    DataColumn(numeric: true, label: Text('最小发电负荷')),
                    DataColumn(numeric: true, label: Text('平均发电负荷')),
                    DataColumn(numeric: true, label: Text('发电负荷率')),
                    DataColumn(numeric: true, label: Text('发电耗水率')),
                    DataColumn(numeric: true, label: Text('发电计划')),
                    DataColumn(numeric: true, label: Text('上网电量')),
                    DataColumn(numeric: true, label: Text('实发电量电量')),
                    DataColumn(numeric: true, label: Text('1号机组运行时间')),
                    DataColumn(numeric: true, label: Text('2号机组运行时间')),
                    DataColumn(numeric: true, label: Text('3号机组运行时间')),
                    DataColumn(numeric: true, label: Text('4号机组运行时间')),
                    DataColumn(numeric: true, label: Text('24时水位')),
                  ], rows: x2)))
        ])));
  }

  Widget getShowItem(String title, double value, int h) {
    return InputWidget.getShowInfo(
        title, Helper.RoundDouble(value, h, ""), Colors.blue);
  }

  DayDataListBody(BuildContext context) => SingleChildScrollView(
        child: DayDataListFields(context),
      );

  DayDataListFields(BuildContext context) {
    return Container(child: Column(children: getWidgetList(context)));
  }
}
